﻿using System;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// 参数验证类
    /// </summary>
    public static class ParaValidate
    {
        /// <summary>
        /// 字符串参数验证
        /// </summary>
        /// <param name="para">参数</param>
        /// <param name="name">参数名</param>
        /// <param name="allowWhiteSpace">是否允许空白字符[true:允许;false:不允许]</param>
        /// <param name="allowEmpty">是否允许为空字符串[true:允许;false:不允许]</param>
        public static void ParaValidateNull(this string para, string name, bool allowWhiteSpace = false, bool allowEmpty = false)
        {
            if (allowWhiteSpace)
            {
                if (allowEmpty)
                {
                    if (para == null)
                    {
                        throw new ArgumentNullException(name);
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(para))
                    {
                        throw new ArgumentNullException(name);
                    }
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(para))
                {
                    throw new ArgumentNullException(name);
                }
            }
        }

        /// <summary>
        /// 参数验证
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="para">参数</param>
        /// <param name="name">参数名</param>
        public static void ParaValidateNull<T>(this T para, string name) where T : class
        {
            if (para == null)
            {
                throw new ArgumentNullException(name);
            }
        }

        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="para">参数</param>
        /// <param name="name">参数名</param>
        public static void IsNullOrWhiteSpace(this string para, string name)
        {
            if (string.IsNullOrWhiteSpace(para))
            {
                throw new ArgumentNullException(name);
            }
        }
    }
}
